package cn.ecnu.paper.mapper;

import cn.ecnu.paper.pojo.Paper;
import cn.ecnu.paper.vo.Pair;
import cn.ecnu.paper.vo.PaperVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface PaperMapper {

    int insertPaper(Paper paper);

    List<PaperVo> getPaperVoByUid(@Param("uid") Integer userId, @Param("startNum") Integer startNum, @Param("pageSize") Integer pageSize);

    @Select("SELECT COUNT(*) FROM paper WHERE poster_id = #{uid} AND flag = 1")
    Integer getPapersCountByUid(@Param("uid") Integer uid);

    @Update("UPDATE paper SET flag = 0 WHERE paper_id = #{paperId}")
    int deletePaperById(@Param("paperId") Integer paperId);

    @Select("SELECT * FROM paper WHERE paper_id = #{paperId} AND flag = 1")
    Paper getPaperById(@Param("paperId") Integer paperId);

    PaperVo getPaperVoByPid(Integer paperId);

    @Select("SELECT COUNT(*) FROM `write` NATURAL JOIN paper WHERE author_id = #{authorId} AND flag = 1")
    int getPapersCountByAuthorId(@Param("authorId") Integer authorId);

    List<PaperVo> getPaperVoByAuthorId(@Param("authorId") Integer authorId, @Param("startNum") int startNum, @Param("pageSize") int pageSize);

    @Select("SELECT COUNT(*) FROM belong NATURAL JOIN paper WHERE field_id = #{fieldId} AND flag = 1")
    int getPapersCountByFieldId(@Param("fieldId") Integer fieldId);

    @Select("SELECT COUNT(*) FROM paper WHERE flag = 1")
    int getPaperCount();

    List<PaperVo> getPaperVoByFiledId(@Param("fieldId") Integer fieldId, @Param("startNum") int startNum, @Param("pageSize") int pageSize);

    int updatePaper(Paper paper);

    @Select("SELECT COUNT(*) FROM note WHERE paper_id = #{paperId} AND flag = 1")
    int getNoteNumByPaperId(@Param("paperId") Integer paperId);


    List<PaperVo> getCollectionByUid(@Param("uid") Integer uid, @Param("startNum") int startNum, @Param("pageSize") int pageSize);

    @Select("SELECT DATE_FORMAT(post_time, '%Y-%m-%d') AS name, COUNT(post_time) AS value FROM paper WHERE poster_id = #{uid} GROUP BY post_time ORDER BY post_time")
    List<Pair> getPaperDateByUid(@Param("uid") Integer uid);

    @Select("SELECT field_name AS name, COUNT(field_name) AS value FROM paper NATURAL JOIN belong NATURAL JOIN field WHERE poster_id = #{uid} GROUP BY field_name ORDER BY field_name")
    List<Pair> getPaperFieldByUid(Integer uid);

    List<PaperVo> getPaperList(@Param("startNum") Integer startNum, @Param("pageSize") Integer pageSize);

    @Select("SELECT field_name AS name, COUNT(field_name) AS value FROM paper NATURAL JOIN belong NATURAL JOIN field WHERE paper.flag = 1 GROUP BY field_name ORDER BY field_name")
    List<Pair> getAllPaperField();

    @Select("SELECT type AS name, COUNT(type) AS value FROM paper WHERE flag = 1 GROUP BY type ORDER BY type")
    List<Pair> getAllPaperDirection();

    List<PaperVo> getPaperALL();
}
